package com.jhgk.jhcee.system.utils;

import cn.hutool.core.util.ReflectUtil;
import cn.smallbun.screw.core.Configuration;
import cn.smallbun.screw.core.engine.EngineConfig;
import cn.smallbun.screw.core.engine.EngineFileType;
import cn.smallbun.screw.core.engine.EngineTemplateType;
import cn.smallbun.screw.core.execute.DocumentationExecute;
import com.jhgk.jhcee.system.domain.model.SysUserInfoModel;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import javax.sql.DataSource;

/**
 * 数据库文档生成  待优化
 * @author: xiezhongping
 * @description:
 * @date: 2021/8/17 9:53.
 */
public class DBDocUtil {

    public final static String driver = "com.mysql.cj.jdbc.Driver";
    public final static String filePath = "doc/db";
    public class JhgkConfig {
        public final static String url = "jdbc:mysql://xpk.jhcee.cn:3306/jhgk";
        public final static String username = "root";
        public final static String password = "1qaz@WSX#EDC";
    }

    public class AuthConfig {
        public final static String url = "jdbc:mysql://xpk.jhcee.cn:3306/auth";
        public final static String username = "root";
        public final static String password = "1qaz@WSX#EDC";
    }

    public class CasConfig {
        public final static String url = "jdbc:mysql://xpk.jhcee.cn:3306/cas";
        public final static String username = "root";
        public final static String password = "1qaz@WSX#EDC";
    }

    public class BfkConfig {
        public final static String url = "jdbc:mysql://221.236.30.65:53306/bfk_pre";
        public final static String username = "root";
        public final static String password = "Asdf~hjkl@1";
    }

    public static void createDoc(String dbname, String url, String username, String password,EngineFileType fileType) {
        //数据源
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setDriverClassName(driver);
        hikariConfig.setJdbcUrl(url);
        hikariConfig.setUsername(username);
        hikariConfig.setPassword(password);
        //设置可以获取tables remarks信息
        hikariConfig.addDataSourceProperty("useInformationSchema", "true");
        hikariConfig.setMinimumIdle(2);
        hikariConfig.setMaximumPoolSize(5);
        DataSource dataSource = new HikariDataSource(hikariConfig);
        //生成配置
        EngineConfig engineConfig = EngineConfig.builder()
                //生成文件路径
                .fileOutputDir(filePath)
                //打开目录
                .openOutputDir(true)
                //文件类型
                .fileType(fileType)
                //生成模板实现
                .produceType(EngineTemplateType.freemarker)
                //自定义文件名称
                .fileName(dbname).build();
        //配置
        Configuration config = Configuration.builder()
                //版本
                .version("1.0.0")
                //描述
                .description("数据库设计文档生成"+"["+dbname+"]")
                //数据源
                .dataSource(dataSource)
                //生成配置
                .engineConfig(engineConfig)
                .build();
        //执行生成
        new DocumentationExecute(config).execute();
    }

    public static void getClassAttribute(Object o,String fieldName) {
        Object fieldValue = ReflectUtil.getFieldValue(o, fieldName);
        String s = String.valueOf(fieldValue);
        System.out.println(s);
    }

    public static void main(String[] args) {
//        createDoc("auth",AuthConfig.url,AuthConfig.username,AuthConfig.password,EngineFileType.WORD);
//        createDoc("jhgk",JhgkConfig.url,JhgkConfig.username,JhgkConfig.password,EngineFileType.MD);
//        createDoc("cas",CasConfig.url,CasConfig.username,CasConfig.password,EngineFileType.MD);
//        createDoc("bfk",BfkConfig.url,BfkConfig.username,BfkConfig.password,EngineFileType.MD);
        SysUserInfoModel model = new SysUserInfoModel();
        model.setUsername("admin");
        getClassAttribute(model,"username");
    }

}
